< Prev(J) / Next(K) > / View :

Filename: /text/sbasic/shared/01020300.xhp

(section: prozedur) (bookmark: bm_id3149456)
procedures
functions;using
variables;passing to procedures and functions
parameters;for procedures and functions
parameters;passing by reference or value
variables;scope
scope of variables
GLOBAL variables
PUBLIC variables
PRIVATE variables
functions;return value type
return value type of functions

手続き
関数;使い方
変数;手続きや関数への変数の渡し方
パラメータ;手続きと関数
パラメータ;参照渡しと値渡し
変数;適用範囲
変数の適用範囲
GLOBAL 変数
PUBLIC 変数
PRIVATE 変数
関数;戻り値の型
関数の戻り値の型

Using Procedures and Functions
手続きおよび関数の使用法(hd_id3149456.1)

The following describes the basic use of procedures and functions in Office Basic.
ここでは、Office Basic でのプロシージャーおよび関数の基本的な使用法について説明します。(par_id3150767.2)

(/section: prozedur)
Note Icon 注マーク When you create a new module, Office Basic automatically inserts a SUB called "Main". This default name has nothing to do with the order or the starting point of a Office Basic project. You can also safely rename this SUB.
モジュールを新規作成すると、Office Basic は自動的に「Main」という名前のプロシージャーを作成します。このデフォルト名は Office Basic プロジェクトの開始位置や順序には関係ありません。このプロシージャー名は任意に変更できます。(par_id3151215.56)

Procedures (SUBS) and functions (FUNCTIONS) help you maintaining a structured overview by separating a program into logical pieces.
プログラムの構造は、プロシージャー (SUB) や関数 (FUNCTION) を使用することで、個々の機能別に整理することができます。(par_id3154124.3)

One benefit of procedures and functions is that, once you have developed a program code containing task components, you can use this code in another project.
また、手続きや関数を使用するメリットの 1 つに、こうした特定の機能を担ったプログラムコード群を他のプロジェクトで再利用できる点があります。(par_id3153193.4)

Passing Variables to Procedures (SUB) and Functions (FUNCTION)
プロシージャー (SUB) や関数 (FUNCTION) への変数の渡し方(hd_id3153770.26)

Variables can be passed to both procedures and functions. The SUB or FUNCTION must be declared to expect parameters:
プロシージャーや関数には、変数を渡すことができます。これらの変数は、SUB および FUNCTION ステートメント部で使用を宣言する必要があります。(par_id3155414.27)

SUB SubName(Parameter1 As Type, Parameter2 As Type,...)
SUB SubName(Parameter1 As Type, Parameter2 As Type,...)(par_id3163710.28)

Program code
Program code(par_id3151114.29)

END SUB
END SUB(par_id3146975.30)

The SUB is called using the following syntax:
こうした SUB の呼び出しは、下記の構文で行います。(par_id3152577.31)

SubName(Value1, Value2,...)
SubName(Value1, Value2,...)(par_id3159154.32)

The parameters passed to a SUB must fit to those specified in the SUB declaration.
SUB に渡す個々の変数は、SUB 宣言部の変数型と一致する必要があります。(par_id3147124.33)

The same process applies to FUNCTIONS. In addition, functions always return a function result. The result of a function is defined by assigning the return value to the function name:
これらの事項は FUNCTION についても同様ですが、この場合は関数の戻り値が返されます。これらの戻り値は、各関数の処理ルーチンが終了するまでに算出しておき、戻り値とする計算結果のパラメータを関数名に代入する形で確定します(例を参照)。(par_id3147397.34)

FUNCTION FunctionName(Parameter1 As Type, Parameter2 As Type,...) As Type
FUNCTION FunctionName(Parameter1 As Type, Parameter2 As Type,...)As Type(par_id3149412.35)

Program code
Program code(par_id3156284.36)

FunctionName=Result
FunctionName=Result(par_id3145799.37)

End Function
End Function(par_id3150716.38)

The FUNCTION is called using the following syntax:
こうした FUNCTION の呼び出しは、下記の構文で行います。(par_id3153839.39)

Variable=FunctionName(Parameter1, Parameter2,...)
Variable=FunctionName(Parameter1, Parameter2,...)(par_id3146914.40)

Tip Icon ヒント You can also use the fully qualified name to call a procedure or function:
Library.Module.Macro()
For example, to call the Autotext macro from the Gimmicks library, use the following command:
Gimmicks.AutoText.Main()

プロシージャまたは関数を呼び出すには、完全修飾名を使うこともできます:
Library. Module. Macro()
たとえば、Grimmicks ライブラリにある Autotext マクロを呼び出すには、以下のようにコマンドを使います:
Gimmicks.AutoText.Main()
(par_idN107B3.)

Passing Variables by Value or Reference
パラメータの参照渡しと値渡し(hd_id3156276.45)

Parameters can be passed to a SUB or a FUNCTION either by reference or by value. Unless otherwise specified, a parameter is always passed by reference. That means that a SUB or a FUNCTION gets the parameter and can read and modify its value.
SUB や FUNCTION に渡すパラメータは、参照渡しか値渡しのいずれかの形で与えることができます。明示的に指定しない限り、パラメータは参照渡しとして渡されます。この場合、SUB や FUNCTION に渡されたパラメータは、その値の読み取りだけでなく、値の変更が可能です。(par_id3155765.47)

If you want to pass a parameter by value insert the key word "ByVal" in front of the parameter when you call a SUB or FUNCTION, for example:
値渡しでパラメータを与えるには、下記の例のように SUB や FUNCTION の呼び出し時に「ByVal」キーワードをパラメータの直前に指定します。(par_id3145640.53)

Result = Function(ByVal Parameter)
Result = Function(ByVal Parameter)(par_id3150042.54)

In this case, the original content of the parameter will not be modified by the FUNCTION since it only gets the value and not the parameter itself.
この場合 FUNCTION 部には、パラメータ本体ではなくその値だけが渡されるため、パラメータ値が書き換えられることはありません。(par_id3149258.55)

Scope of Variables
変数の適用範囲(hd_id3150982.57)

A variable defined within a SUB or FUNCTION, only remains valid until the procedure is exited. This is known as a "local" variable. In many cases, you need a variable to be valid in all procedures, in every module of all libraries, or after a SUB or FUNCTION is exited.
SUB または FUNCTION 内に定義される変数であり、手続きが完了するまでの間だけ有効です。 これは「ローカル」変数です。 多くの場合、すべての手続き内、すべてのライブラリの各モジュール内、または SUB や FUNCTION が完了したあとで、変数を有効にしておく必要があります。(par_id3149814.58)

Declaring Variables Outside a SUB or FUNCTION
SUB や FUNCTION 外部での変数宣言(hd_id3154186.59)

GLOBAL VarName As TYPENAME
GLOBAL VarName As TYPENAME(par_id3150208.111)

The variable is valid as long as the Office session lasts.
こうして宣言した変数は、Office セッションの終了時まで有効となります。(par_id3145258.112)

PUBLIC VarName As TYPENAME
PUBLIC VarName As TYPENAME(par_id3153198.60)

The variable is valid in all modules.
こうして宣言した変数は、すべてのモジュールで有効となります。(par_id3150088.61)

PRIVATE VarName As TYPENAME
PRIVATE VarName As TYPENAME(par_id3158212.62)

The variable is only valid in this module.
こうして宣言した変数は、該当モジュールの内部だけで有効となります。(par_id3152994.63)

DIM VarName As TYPENAME
DIM VarName As TYPENAME(par_id3150886.64)

The variable is only valid in this module.
こうして宣言した変数は、該当モジュールの内部だけで有効となります。(par_id3150368.65)

Example for private variables
プライベート変数の例(hd_id5097506.)

Enforce private variables to be private across modules by setting CompatibilityMode(true).
CompatibilityMode(true) を設定して、プライベート変数がモジュール間でプライベートになるように強制的に設定します。(par_id8738975.)

REM ***** Module1 *****
REM ***** Module1 *****(par_id146488.)

Private myText As String
Private myText As String(par_id2042298.)

Sub initMyText
Sub initMyText(par_id2969756.)

myText = "Hello"
myText = "Hello"(par_id9475997.)

print "in module1 : ", myText
print "in module1 :", myText(par_id6933500.)

End Sub
End Sub(par_id631733.)

REM ***** Module2 *****
REM ***** Module2 *****(par_id8234199.)

'Option Explicit
'Option Explicit(par_id6969512.)

Sub demoBug
Sub demoBug(par_id1196935.)

CompatibilityMode( true )
CompatibilityMode( true )(par_id1423993.)

initMyText
initMyText(par_id6308786.)

' Now returns empty string
' 空の文字列を返します(par_id4104129.)

' (or rises error for Option Explicit)
' (あるいは Option Explicit の場合はエラーを報告します)(par_id7906125.)

print "Now in module2 : ", myText
print "in module1 :", myText(par_id8055970.)

End Sub
End Sub(par_id2806176.)

Saving Variable Content after Exiting a SUB or FUNCTION
SUB や FUNCTION 終了後も値を維持できる変数(hd_id3154368.66)

STATIC VarName As TYPENAME
STATIC VarName As TYPENAME(par_id3156288.67)

The variable retains its value until the next time the FUNCTION or SUB is entered. The declaration must exist inside a SUB or a FUNCTION.
宣言した変数は、次に SUB や FUNCTION を実行するまで有効となります。 この場合の変数宣言は、SUB や FUNCTION の内部に記述する必要があります。(par_id3154486.68)

Specifying the Return Value Type of a FUNCTION
関数の戻り値のデータ型の指定(hd_id3155809.41)

As with variables, include a type-declaration character after the function name, or the type indicated by "As" and the corresponding key word at the end of the parameter list to define the type of the function's return value, for example:
関数の戻り値のデータ型の設定は、変数の場合と同様の型宣言子を用いて行い、下記の例のように、関数宣言時の関数名とパラメータリストに続けて「As」および型宣言用キーワードを記述することで指定します。(par_id3149404.42)

Function WordCount(WordText as String) as Integer
Function WordCount(WordText as String) as Integer(par_id3152899.43)


< Prev / Next >